package com.gongpingjialoan.adapter;

import android.content.Context;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gongpingjialoan.R;
import com.gongpingjialoan.cc.Const;
import com.gongpingjialoan.data.CategoryData;
import com.gongpingjialoan.global.GPJApplication;
import com.gongpingjialoan.imageload.ImageLoad;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/5/18.
 */
public class BrandPopLeftAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater mLayoutInflater;
    JSONArray list;
    CategoryData mCategoryData;

    public BrandPopLeftAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
        mCategoryData = GPJApplication.getInstance().getCategoryData();
    }

    public void setData(JSONArray list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.length();
    }

    @Override
    public JSONObject getItem(int position) {
        try {
            return list.getJSONObject(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_brand_pop_left_list, null);
        }

        JSONObject jo = getItem(position);

//        TextView nameT = (TextView) convertView.findViewById(R.id.brandText);
//
//        try {
//            nameT.setText(jo.getString("brand_slug_zh") + "  ");
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }

        try {

            String content = jo.getString("brand_slug_zh") + "  " + "在售(" + jo.getString("total") + ")辆";
            String des = ToDBC(content);
            TextView desT = (TextView) convertView.findViewById(R.id.des);

            int index = des.indexOf(jo.getString("total"));

            SpannableStringBuilder builder = new SpannableStringBuilder(des);
            ForegroundColorSpan redSpan = new ForegroundColorSpan(mContext.getResources().getColor(R.color.delear_orange));
            builder.setSpan(redSpan, index, index + jo.getString("total").length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            desT.setText(builder);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ImageView picI = (ImageView) convertView.findViewById(R.id.brandLogo);
        try {
            String filename = mCategoryData.getLogoByBrand(jo.getString("brand_slug"));
            String logo_url =
                    Const.IMG_DOMAIN
                            + GPJApplication.getInstance().getApiUrlFromMeta("brand_model_logo_img")
                            + filename + "?imageView2/0/w/100/h/100";
            ImageLoad.LoadImage(picI, logo_url, R.drawable.brandnull, R.drawable.brandnull,mContext);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return convertView;
    }


    public static String ToDBC(String input) {
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375)
                c[i] = (char) (c[i] - 65248);
        }
        return new String(c);
    }
}
